package com.joshua.spring.entity.vo;


import com.fasterxml.jackson.annotation.JsonFormat;
import com.joshua.spring.entity.em.ResponseEnum;
import com.joshua.spring.exception.ExceptionEnumInter;
import com.joshua.spring.util.JacksonUtils;
import lombok.Data;
import org.springframework.http.HttpStatus;
import org.springframework.util.ObjectUtils;

import java.util.Calendar;
import java.util.Date;

/**
 * description
 * package com.joshua.spring.entity.vo
 * classname  GlobalResponse
 *
 * @author <a href="mailto:joshualwork@163.com">joshua_liu</a>
 * @date 2020/3/30 16:17
 */

@Data
public class GlobalResponse<T> {
    private Integer status;
    private boolean success;
    private String code;
    private ErrorMsg errorMsg;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date timestamp;
    private T data;

    @Override
    public String toString() {
        return JacksonUtils.obj2json(this);
    }

    public static GlobalResponse buildSuccess() {
        return buildSuccess(null);
    }

    public static <T> GlobalResponse<T> buildSuccess(T object) {
        GlobalResponse response = buildFormatter(ResponseEnum.SUCCESS);
        if (null != object) {
            response.setData(object);
        }
        return response;
    }

    public static GlobalResponse buildFail(ExceptionEnumInter responseEnum) {
        return buildFormatter(responseEnum);
    }

    public static GlobalResponse buildFail(ExceptionEnumInter responseEnum, String... msg) {
        return buildFormatter(responseEnum, msg);
    }


    public static GlobalResponse buildFail() {
        return buildFail(ResponseEnum.FAIL);
    }

    public static GlobalResponse buildFail(String... msg) {
        return buildFail(ResponseEnum.FAIL, msg);
    }

    public static GlobalResponse buildFail(HttpStatus httpStatus) {
        if (HttpStatus.OK.value() == httpStatus.value()) {
            return buildSuccess();
        }
        ResponseEnum responseEnum = ResponseEnum.getResultEnumByHttpStatus(httpStatus);
        GlobalResponse response = buildFormatter(responseEnum);
        return response;
    }


    private static GlobalResponse buildFormatter(ExceptionEnumInter responseEnum) {
        GlobalResponse response = buildFormatter(responseEnum, null);
        return response;
    }

    private static GlobalResponse buildFormatter(ExceptionEnumInter responseEnum, String... msg) {
        GlobalResponse response = new GlobalResponse();
        response.setSuccess(ResponseEnum.SUCCESS.equals(responseEnum));
        response.setStatus(responseEnum.getHttpStatus().value());
        response.setCode(responseEnum.getCode());
        response.setTimestamp(Calendar.getInstance().getTime());
        if (!ResponseEnum.SUCCESS.equals(responseEnum)) {
            String msgResult = ObjectUtils.isEmpty(msg) ? responseEnum.getMsgFormatter()
                    : String.format(responseEnum.getMsgFormatter(), msg);
            response.setErrorMsg(ErrorMsg.builder().eCode(responseEnum.toString()).eMsg(msgResult)
                    .ePhrase(responseEnum.getHttpStatus().getReasonPhrase()).build());
        }
        return response;
    }


}


